package com.weibo.user.severlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.weibo.common.BaseSeverlet;
import com.weibo.common.MailUtil;

public class MailSendServerlet extends BaseSeverlet {

	private static final Logger logger = Logger
			.getLogger(MailSendServerlet.class);

	@Override
	public void doService(HttpServletRequest request,
			HttpServletResponse response) {

		String email = request.getParameter("email") == null ? "" : request
				.getParameter("email");
		
		if (email.equals("")) {
			try {
				request.getRequestDispatcher("/index.jsp").forward(request,
						response);
			} catch (ServletException e) {
				logger.error("UserServerlet ServletException");
				e.printStackTrace();
			} catch (IOException e) {
				logger.error("UserServerlet IOException");
				e.printStackTrace();
			}
		}
		
		String registerId = "" + Math.random() * Math.random();

		String basepath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort()
				+ getServletContext().getContextPath() + "/";
		String registerurl = basepath + "MailBackServlet.do?registerId="
				+ registerId;// 待会用户点在邮箱中点击这个链接回到你的网站。

		logger.info("再次发送验证邮件！");
		MailUtil.sendMail(email, registerurl);
		try {
			request.setAttribute("email", email);
			request.getRequestDispatcher("/WEB-INF/login/Login_success.jsp")
					.forward(request, response);
		} catch (ServletException e) {
			logger.error("UserServerlet ServletException");
			e.printStackTrace();
		} catch (IOException e) {
			logger.error("UserServerlet IOException");
			e.printStackTrace();
		}
	}

}
